<?php

App::uses('PaymentAppController', PAYMENT_CAMELCASENAME.'.Controller');

class PaymentController extends PaymentAppController{
	
	var $uses;
	
	public function __construct($request, $response){
		$this->uses = PAYMENT_CAMELCASENAME.'.PaymentPagamento';
		parent::__construct($request, $response);
	}
	
	public function beforeFilter(){
		$this->Auth->allow('notification');
		parent::beforeFilter();
	}
	
	public function index(){
		$config = $this->Session->read('PaymentPlugin');
		$model = $config['model'];
		$this->loadModel($model);
		$payment_data = $this->$model->find('first', array('conditions'=>array($model.'.'.$config['id_field']=>$config['id'])));
		$this->Session->write(PAYMENT_CAMELCASENAME.'.data', $payment_data[$model]);
		$this->PaymentPagamento->createTransaction($payment_data, $config);
		$this->Session->write('PaymentPlugin.id_plugin_payment', $this->PaymentPagamento->id);
		$this->redirect(array('action'=>'choose'));
	}
	
	public function choose($id = null){
		if(is_null($id)){
			$this->set('gateways', $this->PaymentPagamento->PaymentGateway->activeList());
		}
		else{
			if($this->PaymentPagamento->updateGateway($id, $this->Session->read('PaymentPlugin.id_plugin_payment')))
				$this->redirect(array('action'=>'redirectTo', $id));
		}
	}
	
	public function redirectTo($id){
		$this->loadModel(PAYMENT_CAMELCASENAME.'.PaymentGateway');
		$gateway = $this->PaymentGateway->findById($id);
		$component = $gateway['PaymentGateway']['model'];
		$this->$component = $this->Components->load(PAYMENT_CAMELCASENAME.'.'.$component);
		$this->$component->setUpTransaction($id, $this->Session->read('PaymentPlugin.id'), $this->Session->read('PaymentPlugin.items'));
		$result = $this->$component->sendTransaction();
		if($result){
			if($this->PaymentPagamento->saveRedirectCodeAndDate($this->Session->read('PaymentPlugin.id_plugin_payment'), $result)){
// 				echo $this->payment_link.$result['code'];
				if(!isset($result['display'])){
					$this->redirect($result['code']);
				}
				else{
					$this->set('data', $result['data']);
					$this->render('display_'.$result['display']);
				}
			}
		}
	}
	
	public function gateway_return(){
		// TODO mandar para o Component para ficar generico
		$transaction = $this->request->query['id_transacao'];
		if($this->PaymentPagamento->saveTransactionCode($this->Session->read('PaymentPlugin.id_plugin_payment'), $transaction)){
			$this->Session->setFlash(__('Seu pagamento foi registrado com sucesso!
					Acompanhe a atualização do PagSeguro por seu E-mail!'));
			$redirect = $this->Session->read('PaymentPlugin.redirect_url');
			$redirect[] = $transaction;
			$this->redirect($redirect);
		}
	}
	
	public function notification($gateway, $id = null){
		$this->autoRender = false;
		$this->component = $this->Components->load(PAYMENT_CAMELCASENAME.'.'.$gateway);
		$dataToSave = $this->component->notification($this->request->data, $id);
		$this->loadModel(PAYMENT_CAMELCASENAME.'.PaymentNotification');
		$gateway_data = $this->PaymentNotification->Gateway->findByModel($gateway);
		$dataToSave['gateway_id'] = $gateway_data['Gateway']['id']; 
		$this->PaymentNotification->saveNotification($dataToSave);
		if(isset($dataToSave['redirect'])){
			$this->redirect($dataToSave['redirect']);
		}
	}
	
	public function configuration($gateway_id = null){
		if($this->isAdmin()){
			if(is_null($gateway_id)){
				$this->set('gateways', $this->PaymentPagamento->PaymentGateway->activeList());
				$this->set('not_active_gateways', $this->PaymentPagamento->PaymentGateway->notActiveList());
				$this->render('configuration_gateways_list');
			}
			else{
				if(!empty($this->request->data)){
					if($this->PaymentPagamento->PaymentGateway->saveConfig($this->request->data, $gateway_id)){
						$this->Session->setFlash(__d('PaymentPlugin', 'Configurações atualizadas com sucesso'));
						$this->redirect(array('action'=>'configuration'));
					}
				}
				else{
					$this->set('config', $this->PaymentPagamento->PaymentGateway->config($gateway_id));
					$name = $this->PaymentPagamento->PaymentGateway->gatewayName($gateway_id);
					$model = $this->PaymentPagamento->PaymentGateway->gatewayModel($gateway_id);
					$this->set('name', $name);
					$this->render($model);
				}
			}
		}
		else $this->notAuth();
	}
	
	public function selectGateway($gateway_id){
		if($this->isAdmin()){
			if($this->PaymentPagamento->PaymentGateway->select($gateway_id)){
				$this->Session->setFlash(__d('PaymentPlugin', 'gateway_ativado_por_favor_verifique_as_configuracoes'));
				$this->redirect(array('action'=>'configuration', $gateway_id));
			}
			else{
				$this->Session->setFlash(__d('PaymentPlugin', 'gateway_nao_ativado_por_favor_tente_novamente'));
				$this->redirect(array('action'=>'configuration'));
			}
		}
	}
	
	public function deselectGateway($gateway_id){
		if($this->isAdmin()){
			if($this->PaymentPagamento->PaymentGateway->select($gateway_id)){
				$this->Session->setFlash(__d('PaymentPlugin', 'gateway_desativado'));
				$this->redirect(array('action'=>'configuration'));
			}
			else{
				$this->Session->setFlash(__d('PaymentPlugin', 'gateway_nao_desativado_por_favor_tente_novamente'));
				$this->redirect(array('action'=>'configuration'));
			}
		}
	}
	
}
